﻿function Comments() {
    var self = this;
    this.collection = ko.observableArray();
    this.selectAll = ko.observable(false);

    this.successMessage = ko.observable();
    this.errors = ko.observableArray();
    this.errorMessage = ko.computed({
        read: function () {
            var html = '';
            if (this.errors().length > 0)
                html += this.errors()[0];
            if (this.errors().length > 1) {
                html += "<ul>"
                for (var idx = 1; idx < this.errors().length; idx++)
                    html += "<li>" + this.errors()[idx] + "</li>";
                html += "</ul>";
            }
            return html;
        },
        write: function (value) {
            this.errors.removeAll();
            this.errors.push(value);
        },
        owner: this
    });

    this.loadJson = function (data) {
        for (var idx = 0; idx < data.length; idx++) {
            var elem = data[idx];
            var item = new Comment(this);
            item.isOdd(idx % 2 == 0);
            item.load(elem);
            this.collection.push(item);
        }
    }

    this.redoOdd = function () {
        for (var idx = 0; idx < this.collection().length; idx++) {
            var item = this.collection()[idx];
            item.isOdd(idx % 2 == 0);
        }
    }

    this.get = function (id) {
        for (var idx = 0; idx < this.collection().length; idx++) {
            if (this.collection()[idx].id() == id)
                return this.collection()[idx];
        }
    }

    this.remove = function (removeItem) {
        this.collection.remove(function (item) { return item.id() == removeItem.id() });
    }

    this.select = function () {
        for (var idx = 0; idx < this.collection().length; idx++) {
            this.collection()[idx].selected(this.selectAll());
        }
        return true;
    }

    this.updateSelected = function () {
        var selectAll = true;
        for (var idx = 0; idx < this.collection().length; idx++) {
            selectAll = selectAll && this.collection()[idx].selected();
        }
        this.selectAll(selectAll);
    }

    this.getSelected = function () {
        var array = new Array();
        for (var idx = 0; idx < this.collection().length; idx++) {
            if (this.collection()[idx].selected())
                array.push(this.collection()[idx].id());
        }
        return array.join();
    }

    this.removeSelected = function () {
        for (var idx = 0; idx < this.collection().length; idx++) {
            if (this.collection()[idx].selected()) {
                this.collection.remove(this.collection()[idx]);
                idx--;
            }
        }
    }

    this.deleteComments = function () {
        var csvIds = this.getSelected();
        if (csvIds.length == 0) {
            alert('Please select a comment');
        }
        else {
            var url = appPath + 'Comment/Delete/' + csvIds;
            $.get(url, function (data) {
                if (data.Result) {
                    self.successMessage(data.Message);
                    setInterval(function () { self.successMessage('') }, settings.fadeOutInterval);
                    self.errors.removeAll();
                    self.removeSelected();
                    self.redoOdd();
                } else {
                    self.errorMessage(data.Message);
                }
            });
        }
    }

    this.deleteEnabled = ko.computed(function () {
        return this.collection().length > 0;
    }, this);

    this.approveComments = function () {
        var csvIds = this.getSelected();
        if (csvIds.length == 0) {
            alert('Please select a comment');
        }
        else {
            var url = appPath + 'Comment/Approve/' + csvIds;
            $.get(url, function (data) {
                if (data.Result) {
                    self.successMessage(data.Message);
                    setInterval(function () { self.successMessage('') }, settings.fadeOutInterval);
                    self.errors.removeAll();
                    for (var idx = 0; idx < self.collection().length; idx++) {
                        if (self.collection()[idx].selected()) {
                            self.collection()[idx].isApproved(true);
                            self.collection()[idx].selected(false);
                        }
                    }
                } else {
                    self.errorMessage(data.Message);
                }
            });
        }
    }

    this.rejectComments = function () {
        var csvIds = this.getSelected();
        if (csvIds.length == 0) {
            alert('Please select a comment');
        }
        else {
            var url = appPath + 'Comment/Reject/' + csvIds;
            $.get(url, function (data) {
                if (data.Result) {
                    self.successMessage(data.Message);
                    setInterval(function () { self.successMessage('') }, settings.fadeOutInterval);
                    self.errors.removeAll();
                    self.removeSelected();
                    self.redoOdd();
                } else {
                    self.errorMessage(data.Message);
                }
            });
        }
    }

    this.rejectEnabled = ko.computed(function () {
        var unmoderated = false;
        for (var idx = 0; idx < this.collection().length; idx++) {
            unmoderated = unmoderated || !this.collection()[idx].isApproved();
            if (this.collection()[idx].selected() && this.collection()[idx].isApproved())
                return false; // do not reject already approved, only delete!
        }
        return unmoderated;
    }, this);
}